﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Json;

namespace FeaturePlusInWeb.Model
{
    public class ObjectReflactor
    {
        public static T PopulateEntityFromCollection<T>(JsonValue collection) where T : new()
        {
            var entity = new T();
            //得到类型
            Type type = typeof(T);
            //取得属性集合
            PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo p in pi)
            {
                //给属性赋值
                string item = collection[p.Name];

                if (p.PropertyType == typeof(string))
                {
                    if (item != null)
                    {

                        if ((p.Name.Contains("Link") || p.Name.Contains("Signpic")) && !item.ToString().Contains("http://"))
                        {
                            item = Config.SiteDoMain + item;
                        }
                    }
                    else
                    {
                        item = string.Empty;
                    }

                }
                else if(item == null)
                {
                    continue;
                }
                p.SetValue(entity, Convert.ChangeType(item, p.PropertyType, null), null);
            }
            return entity;
        }
    }
}
